// JavaScript Document

//------------------------Routines-------------------------
function EditBook(book_id) {
    //	call the removing routine
    $("div#book-add-edit-form").toggle(true);
    $("div#book-add-edit-form").attr("edit", "true");
    $("div#book-add-edit-form").attr("book_id", book_id);

    //TODO: Take a look how I can parse the JSON formatted returned values;
    var dataString = 'bookid=' + book_id;
    //var book_id = $(this).attr("value");
    $.ajax({
        type: "POST",
        url: "books/get_book_details",
        data: dataString,
        success: function (msg) {

            json_return = eval('(' + msg + ')');

            $('input#title').val(json_return.title);
            $('input#original-title').val(json_return.original);
            $('input#author').val(json_return.author);
            $('input#author').attr('auth_id', json_return.author_id);
            $('input#publisher').val(json_return.publisher);
            $('input#publisher').attr('pub_id', json_return.publisher_id);
            $('input#genre-tags').importTags(json_return.genres);
            $('input#pages').val(json_return.pages);

            var vote_value = json_return.vote;
            //First I set them all not selected so that I start from a blank situation
            for (i = 0; i <= 10; i++) {
                $('div#' + i).attr('class', 'star-rating rater-0 star star-rating-applied star-rating-live');
                var cur = i - 1;
                $('td#vote-value input:radio:nth(' + cur + ')').removeAttr('checked');
            }
            //Now I set the currect value
            for (i = 0; i <= vote_value; i++) {
                $('div#' + i).attr('class', 'star-rating rater-0 star star-rating-applied star-rating-live star-rating-on');
            }
            vote_value--;
            $('td#vote-value input:radio:nth(' + vote_value + ')').attr('checked', 'checked');

            var height = parseInt($('td#tmp-cover').css('height'));
            $('img#show-cover').css('height', (height - 5) + 'px');
            $('img#show-cover').attr('src', json_return.cover);

            $('input#year').val(json_return.year);
            $('input#buyed').val(json_return.buyed);

            $('textarea#description').val(json_return.description);

        },
        error: function (ob, errStr) {
            alert('There was an error editing the book. Please try again.');
        }
    });

    $('input#title').focus();

} 

//------------------------------------------

//Jquery calls
$('li#book-button.button').live("hover", function() {
    // change the state of the "#idd"
    $(this).css('cursor','pointer');
});

// Routines for the readings buttons
$('div#book-reads').live("mouseenter", function () {
    // change the state of the "#idd"
    $(this).css('background-image', 'url(\'/assets/images/icons/book-bookmark-hover.png\')');
});

$('div#book-reads').live("mouseleave", function () {
    // change the state of the "#idd"
    $(this).css('background-image', 'url(\'/assets/images/icons/book-bookmark.png\')');
});

$('div#book-reads').live("click", function () {
    //alert("To be implemented");
    
    var dataString = 'bookid='+ $(this).attr("value");
  	$.ajax({
  		type: "POST",
  		url: "books/get_books_reads",
  		data: dataString,
  		success: function(msg) {
        
  			$('div#book-reads-form').html(msg);
        
        $('#book-reads-form').dialog('open');
        return false;
  
  		},
  		error: function(ob,errStr) {
  			alert('There was an error retrieving the book reads. Please try again.');
  		}
  	});
    
});
//---------------------------------
    
// Routines for the deletes buttons
$('div#book-delete').live("mouseenter", function() {
    // change the state of the "#idd"
    $(this).css('background-image','url(\'/assets/images/icons/book-minus-hover.png\')');
});
  
$('div#book-delete').live("mouseleave", function() {
    // change the state of the "#idd"
    $(this).css('background-image','url(\'/assets/images/icons/book-minus.png\')');
});
  
$('div#book-delete').live("click", function() {		
//	call the removing routine
    var dataString = 'bookid='+ $(this).attr("value");
	$.ajax({
		type: "POST",
		url: "books/delete_book",
		data: dataString,
		success: function(msg) {

			$('div#body-container').html(msg);

		},
		error: function(ob,errStr) {
			alert('There was an error deleting the book. Please try again.');
		}
	});
		
	return false;
});
  
//---------------------------------

//----------ADD/EDIT---------------
$('li#book-button.button').live("click", function () {
    $("div#book-add-edit-form").attr("edit", "false");
    $("div#book-add-edit-form").attr("book_id", "");

    $("div#book-add-edit-form input#title").focus();

    $('#book-add-edit-form').dialog('open');
    return false;
});

$('div#book-edit').live("click", function () {
    EditBook($(this).attr("value"));
    $('#book-add-edit-form').dialog('open');
    return false;
});
//---------------------------------

// Routines for the edit buttons
$('div#book-edit').live("mouseenter", function() {
    // change the state of the "#idd"
    $(this).css('background-image','url(\'/assets/images/icons/book-pencil-hover.png\')');
});
  
$('div#book-edit').live("mouseleave", function() {
    // change the state of the "#idd"
    $(this).css('background-image','url(\'/assets/images/icons/book-pencil.png\')');
});
//---------------------------------

//-------Cover delete------------
$('td#delete-cover').live('click',function(){
    var height = parseInt($('td#tmp-cover').css('height'));
    $('img#show-cover').css('height', (height-5)+'px');
    $('img#show-cover').attr('src','/assets/covers/nocover.cvr');
});
//---------------------------------
  
//------------------SEARCH----------------------
$('li#search-button.button').live("click", function() {
    // change the state of the element
    $('input#title-search').focus();

    $('#book-search-form').dialog('open');
    return false;
});
//------------------------------------------

$('li#all-button.button').live("click", function() {
    // change the state of the "#idd"
    show_view('/main/books');
});
  
$('li#book-button.button').live("hover", function() {
    // change the state of the "#idd"
    $(this).css('cursor','pointer');
});

//---------DROP DOWN AND BOOK READING--------------
$('a.add-new-read').live("click",function() {
  book_id = $(this).parent().parent().attr('book_id');
	$('tr.new-read').before('<tr class="insert-read" kind="new" book_id="'+book_id+'" read_id=""><td id="label" ><label for="status">Status:</label></td><td id="value" style="width:25%;" ><select name="status" id="status"><option value="0" selected="selected">To be read</option><option value="1">Reading</option><option value="2">Finished</option><option value="3">Not Finished</option><option value="4">Abandoned</option><option value="5">Consultation book</option></select></td>		<td id="date_from" style="visibility:hidden;"><label for="datefrom">Started:</label><input type="text" name="datefrom" style="width:55%" value="" id="datefrom"  /></td>		<td id="date_to" style="visibility:hidden;"><label for="dateto">Finished:</label><input type="text" name="dateto" style="width:55%" value="" id="dateto"  /></td>	</tr>');
});
	
$('select#status').live('change', function () {
	switch ($(this).val()) {
	    case '0':
	        $(this).parent().parent().find('td#date_from').css('visibility', 'hidden');
	        $(this).parent().parent().find('td#date_to').css('visibility', 'hidden');
	        $(this).parent().parent().find('td#delete_read').css('visibility', 'hidden');
	        break;

	    case '1':
	        $(this).parent().parent().find('td#date_from').css('visibility', 'visible');
	        $(this).parent().parent().find('td#date_to').css('visibility', 'hidden');
	        $(this).parent().parent().find('td#delete_read').css('visibility', 'visible');
	        break;

	    case '2':
	        $(this).parent().parent().find('td#date_from').css('visibility', 'visible');
	        $(this).parent().parent().find('td#date_to').css('visibility', 'visible');
	        $(this).parent().parent().find('td#delete_read').css('visibility', 'visible');
	        break;

	    case '3':
	        $(this).parent().parent().find('td#date_from').css('visibility', 'visible');
	        $(this).parent().parent().find('td#date_to').css('visibility', 'hidden');
	        $(this).parent().parent().find('td#delete_read').css('visibility', 'visible');
	        break;

	    case '4':
	        $(this).parent().parent().find('td#date_from').css('visibility', 'visible');
	        $(this).parent().parent().find('td#date_to').css('visibility', 'visible');
	        $(this).parent().parent().find('td#delete_read').css('visibility', 'visible');
	        break;

	    case '5':
	        $(this).parent().parent().find('td#date_from').css('visibility', 'hidden');
	        $(this).parent().parent().find('td#date_to').css('visibility', 'hidden');
	        $(this).parent().parent().find('td#delete_read').css('visibility', 'visible');
	        break;

	}
});
//-------------------------

